package function.search;

import graphics.SearchInput2;

import java.util.Timer;
import java.util.TimerTask;

import javax.microedition.lcdui.Canvas;

import tool.KeyboardUtils;

public class SearchKeyHandler2 {

	private static final int NOT_A_KEY = -1001;
	private static final char[] KEYBOARD_CHARS = new char[] { 'a', 'd', 'g',
			'j', 'm', 'p', 't', 'w' };
	private static final int TIME_BETWEEN_TWO_STROKES = 500;

	private char[] letters;
	private int[] keys;
	private int currentCharIndix = 0;
	private Timer keyTimer = null;
	private SearchInput2 searchInput;
	private int length;

	private EventListener[] listeners;
	private int currentKeyCode;
	private Canvas parent;

	public SearchKeyHandler2(int aLength, SearchInput2 aSearchInput,
			Canvas aParent) {
		parent = aParent;
		listeners = new EventListener[10];
		length = aLength;
		searchInput = aSearchInput;
		letters = new char[length];
		keys = new int[length];

		for (int i = 0; i < letters.length; i++) {
			keys[i] = NOT_A_KEY;
		}
	}

	public void keyPressed(int keyCode) {

		if (((keyCode < 48) || (keyCode > 57))
				|| (currentCharIndix >= letters.length)) {
			return;
		}
		if (keyTimer == null) {
			makeNewLetter(keyCode);
		} else if (keyCode != currentKeyCode) {
			if (currentCharIndix < length - 1) {				
				moveCursor();
				if (keyTimer != null) {
					keyTimer.cancel();
				}
				currentCharIndix++;
				makeNewLetter(keyCode);
			} else {
				return;
			}
		} else if (keyCode == currentKeyCode) {
			if (letters[currentCharIndix] != getNextLetter(letters[currentCharIndix])) {
				keyTimer.cancel();
				keyTimer = null;
				letters[currentCharIndix] = getNextLetter(letters[currentCharIndix]);
				startKeyTimer();
			} else {
				return;
			}
		}
		searchInput.charInput(letters[currentCharIndix]);
		parent.repaint();
	}

	private void makeNewLetter(int keyCode) {
		currentKeyCode = keyCode;
		char letter = getLetter(keyCode);
		keys[currentCharIndix] = keyCode;
		letters[currentCharIndix] = letter;
		startKeyTimer();
	}

	private void startKeyTimer() {
		keyTimer = new Timer();
		keyTimer.schedule(new TimerTask() {

			public void run() {
				if (currentCharIndix < length - 1) {
					currentCharIndix++;
					moveCursor();
					keyTimer = null;
				} else {
					searchInput.freeze();
					for (int i = 0; (listeners[i] != null)
							&& (i < listeners.length); i++) {
						listeners[i]
								.processEvent(EventListener.EVENT_PROCESS_DONE);
					}
				}
			}
		}, TIME_BETWEEN_TWO_STROKES);
	}

	private char getLetter(int keyCode) {
		if (keyCode == 48) {
			return ' ';
		} else if (keyCode == 49) {
			return '1';
		}
		return KEYBOARD_CHARS[(keyCode - 50)];
	}

	private char getNextLetter(char letter) {
		return KeyboardUtils.getNextLetterNoNum(letter);
	}

	private void moveCursor() {
		if (currentCharIndix < letters.length) {
			searchInput.shiftCursor(1);
		}
	}

	public void addListener(EventListener listener) {
		int i = 0;
		while ((listeners[i] != null) && (i < listeners.length)) {
			i++;
		}
		listeners[i] = listener;
	}

	public String getSearchInput() {
		return String.valueOf(letters);
	}
}
